clear;clc;

Folder      =   'TableGraphs\\DifferentIntegration\\';
mkdir(Folder);

load('Results_DifferentIntegrationDegree.mat');

%% Collect the Parameter Values
Num_Fin     =   length(SOLUTION_Fin);
Num_Real    =   length(SOLUTION_Real);

XAxis_Fin   =   zeros(Num_Fin,1);
XAxis_Real  =   zeros(Num_Real,1);

for ii=1:Num_Fin
    XAxis_Fin(ii)   =   SOLUTION_Fin{ii}.PP.ExoState.Idio_FI.InvDist(2)*100;
end

for ii=1:Num_Real
    XAxis_Real(ii)  =   SOLUTION_Real{ii}.PP.ExoState.Idio_RI.InvDist(2)*100;
end
%% Collect the Moments
Mom_Fin     =   SubFun_ResultCell2Mom(SOLUTION_Fin);
Mom_Real    =   SubFun_ResultCell2Mom(SOLUTION_Real);

%% Figures
List_Shock  =   fieldnames(Mom_Fin.Stat);
N_col       =   1;
N_row       =   1;
fig_size    =   [N_col,N_row].*[1,0.75]*1/2.5; 
fig_gap     =   [0.02,0.02];
fig_VMargin =   [0.02,0.02];
fig_HMargin =   [0.15,0.02];
%--------------------------------------------------------------------------
% Aggregate
%--------------------------------------------------------------------------
fig_SubPlot =   @(ii_vv)subtightplot(N_row,N_col,ii_vv,...
                                     fig_gap,fig_VMargin,fig_HMargin);

XLabel      =   @(Label)xlabel(Label,'fontsize',8,'interpreter','latex');
YLabel      =   @(Label)ylabel(Label,'fontsize',8,'interpreter','latex');


AxisSetup   =   GR_Axis('TickFmt',struct('X','%g','Y','%.2g'),...
                       'FontSize',struct('X',8,'Y',8),...
                       'Exponent',struct('X',0,'Y',0),...
                       'Box','off');

Tick_X      =   @(X)GR_TickValue('Unit',25).Setup(X);
Tick_Y      =   @(Y)GR_TickValue('Symmetric',0,'MinUnit',0.01).Setup(Y);

LabelList   =   {'Consumption','Inflation','Exchange Rate'};
Line_List   =   {GR_Line('Style','-','Color',MyColor('Black'),'Width',3);...
                 GR_Line('Style','--','Color',MyColor('Blue'),'Width',2);...
                 GR_Line('Style',':','Color',MyColor('Red'),'Width',2) ...
                 };
             
LEG         =   @(PH,LabelList)legend(PH,LabelList,'fontsize',6,...
                                    'interpreter','latex','location','southoutside',...
                                    'NumColumns',2);
XLabel_Fin  =   'Fraction of Financially Integrated Households (\%)';
XLabel_Real =   'Fraction of Real Integrated Households (\%)';
for ii_Sh=1:length(List_Shock)
    SH          =   List_Shock{ii_Sh};
    
    % by Fin
    Fig         =   FigureSetup(['Aggregate, By Fin, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Fin.Stat.(SH)(1:3,:);
    [PH,ax]     =   Plot_Line(XAxis_Fin',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Fin),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Fin),max(XAxis_Fin)];
    XLabel(XLabel_Fin);
    YLabel('Peak Responses (pp)');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'Agg_byFI_',SH]);
    close(Fig);
    
    % by Real
    Fig         =   FigureSetup(['Aggregate, By Real, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Real.Stat.(SH)(1:3,:);
    [PH,ax]     =   Plot_Line(XAxis_Real',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Fin),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Real),max(XAxis_Real)];
    XLabel(XLabel_Real);
    YLabel('Peak Responses (pp)');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'Agg_byRI_',SH]);
    close(Fig);
end


%--------------------------------------------------------------------------
% Distributional
%--------------------------------------------------------------------------
LabelList   =   {'Std.','Gap by R.I.','Gap by F.I.','Gap by N.W.'};
Line_List   =   {GR_Line('Style','-','Color',MyColor('Black'),'Width',3);...
                 GR_Line('Style','--','Color',MyColor('Blue'),'Width',2);...
                 GR_Line('Style','-.','Color',MyColor('Red'),'Width',2); ...
                 GR_Line('Style',':','Color',MyColor('Green'),'Width',2) ...
                 };
for ii_Sh=1:length(List_Shock)
    SH          =   List_Shock{ii_Sh};
    
    %=======
    % by Fin
    %=======
    % Non-Normalized
    Fig         =   FigureSetup(['Dispersion, Non-normalized, By Fin, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Fin.Stat.(SH)(4:7,:);
    
    [PH,ax]     =   Plot_Line(XAxis_Fin',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Fin),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Fin),max(XAxis_Fin)];
    XLabel(XLabel_Fin);
    YLabel('Consumption Response Dispersion (\%)');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'ConsDisp_NonNorm_byFI_',SH]);
    close(Fig);
    % Normalized
    Fig         =   FigureSetup(['Dispersion, Normalized, By Fin, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Fin.Stat.(SH)(4:7,:)./Mom_Fin.Stat.(SH)(1,:);
    
    [PH,ax]     =   Plot_Line(XAxis_Fin',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Fin),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Fin),max(XAxis_Fin)];
    XLabel(XLabel_Fin);
    YLabel('Consumption Response Dispersion');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'ConsDisp_Norm_byFI_',SH]);
    close(Fig);
    %========
    % by Real
    %========
    % Non-Normalized
    Fig         =   FigureSetup(['Dispersion, Non-normalized, By Real, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Real.Stat.(SH)(4:7,:);
    
    [PH,ax]     =   Plot_Line(XAxis_Real',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Real),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Real),max(XAxis_Real)];
    XLabel(XLabel_Real);
    YLabel('Consumption Response Dispersion (\%)');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'ConsDisp_NonNorm_byRI_',SH]);
    close(Fig);
    % Normalized
    Fig         =   FigureSetup(['Dispersion, Normalized, By Real, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Real.Stat.(SH)(4:7,:)./Mom_Real.Stat.(SH)(1,:);
    
    [PH,ax]     =   Plot_Line(XAxis_Real',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Real),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Real),max(XAxis_Real)];
    XLabel(XLabel_Real);
    YLabel('Consumption Response Dispersion');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'ConsDisp_Norm_byRI_',SH]);
    close(Fig);
end

%--------------------------------------------------------------------------
% Distributional: Std, Normalized vs. Non-normalized
%--------------------------------------------------------------------------
N_col       =   1;
N_row       =   1;
fig_size    =   [N_col,N_row].*[1,0.75]*1/2.5; 
fig_gap     =   [0.02,0.02];
fig_VMargin =   [0.15,0.02];
fig_HMargin =   [0.10,0.10]; 

fig_SubPlot =   @(ii_vv)subtightplot(N_row,N_col,ii_vv,...
                                     fig_gap,fig_VMargin,fig_HMargin);
AxisSetup   =   GR_Axis_2Y('TickFmt',struct('X','%g','Y',{{'%.2g','%.2g'}}),...
                           'FontSize',struct('X',8,'Y',[8,8]),...
                           'Exponent',struct('X',0,'Y',[0,0]),...
                           'Box','off');
LEG         =   @(PH,LabelList)legend(PH,LabelList,'fontsize',6,...
                                    'interpreter','latex','location','north',...
                                    'NumColumns',2);
Tick_Y      =   @(Y)GR_TickValue('Symmetric',0,'MinUnit',0.01).Setup(Y);

Line_NonNorm=   GR_Line('Style','--','Color',MyColor('Gray'),'Width',2);
Line_Norm   =   GR_Line('Style','-','Color',MyColor('Black'),'Width',3);
for ii_Sh=1:length(List_Shock)
    SH          =   List_Shock{ii_Sh};

    % by Fin
    Fig         =   FigureSetup(['Dispersion, Combined, By Fin, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom_NonNorm=Mom_Fin.Stat.(SH)(4,:);
    TempMom_Norm=   TempMom_NonNorm./Mom_Fin.Stat.(SH)(1,:);
    
    [PH_1,ax]   =   Plot_Line(XAxis_Fin,TempMom_Norm,...
                              'ax',ax,'LineProperty',Line_Norm,...
                              'XTick',Tick_X(XAxis_Fin),'YTick',Tick_Y(TempMom_Norm));
%     YLabel('Std. of consumption responses, normalized');
    yyaxis right;
    [PH_2,ax]   =   Plot_Line(XAxis_Fin,TempMom_NonNorm,...
                              'ax',ax,'LineProperty',Line_NonNorm,...
                              'XTick',Tick_X(XAxis_Fin),'YTick',Tick_Y(TempMom_NonNorm));
%     YLabel('Std. of consumption responses, non-normalized (pp)');
    XLabel(XLabel_Fin);
    ax.XLim     =   [min(XAxis_Fin),max(XAxis_Fin)];
    LEG([PH_1,PH_2],{'Normalized, left','Non-normalized, right'});
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'ConsDisp_byFI_',SH]);
    close(Fig);
    % by Real
    Fig         =   FigureSetup(['Dispersion, Combined, By Real, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom_NonNorm=Mom_Real.Stat.(SH)(4,:);
    TempMom_Norm=   TempMom_NonNorm./Mom_Real.Stat.(SH)(1,:);
    
    [PH_1,ax]   =   Plot_Line(XAxis_Real,TempMom_Norm,...
                              'ax',ax,'LineProperty',Line_Norm,...
                              'XTick',Tick_X(XAxis_Real),'YTick',Tick_Y(TempMom_Norm));
    yyaxis right;
    [PH_2,ax]   =   Plot_Line(XAxis_Real,TempMom_NonNorm,...
                              'ax',ax,'LineProperty',Line_NonNorm,...
                              'XTick',Tick_X(XAxis_Real),'YTick',Tick_Y(TempMom_NonNorm));
    XLabel(XLabel_Real);
    ax.XLim     =   [min(XAxis_Real),max(XAxis_Real)];
    LEG([PH_1,PH_2],{'Normalized, left','Non-normalized, right'});
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'ConsDisp_byRI_',SH]);
    close(Fig);
end